# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------#
# ZhihuiJin <zhihuijin@gmail.com>
# vim: ts=4 sw=4 sts=4 et tw=78:
# Fri Dec  8 14:46:39 2006
#
#----------------------------------------------------------------------------#

""" Han word  data analysis report
"""

#----------------------------------------------------------------------------#

import hanChar
import hanWord
import util

# charset
commHanzi        = hanChar.getChCommonChar()
commKanji        = hanChar.getJpCommonChar()
sharedChar       = hanChar.getSharedCommonChar()
baselineHanzi    = hanChar.getReadableHanziBaseline()
baselineKanji    = hanChar.getReadableKanjiBaseline()
toplineHanzi     = hanChar.getReadableHanziTopline()
toplineKanji     = hanChar.getReadableKanjiTopline()

# word list
chAllWord       = hanWord.getChHanWordList()
jpAllWord       = hanWord.getJpHanWordList()

# word filter flag
commOnly = 1

if commOnly:
    chWord = hanWord.filterByCharset(chAllWord, commHanzi)
    jpWord = hanWord.filterByCharset(jpAllWord, commKanji)
else :
    chWord = chAllWord
    jpWord = jpAllWord


#----------------------------------------------------------------------------#
def percent(subList, allList):
    return '%d\t%.3f' % (len(subList), 1.0 * len(subList) / len(allList))

#----------------------------------------------------------------------------#
def readableReport(readableHanzi, readableKanji):
    # ch word
    readableChWord   = hanWord.filterByCharset(chWord, readableHanzi)
    unreadableChWord = hanWord.reverseFilterByCharset(chWord, readableHanzi)
    partialChWord    = list(set(chWord) - set(readableChWord) - set(unreadableChWord))
    readableHanziPer = '%.3f' % hanWord.charPercentInWord(readableHanzi, chWord)

    #jp word
    readableJpWord   = hanWord.filterByCharset(jpWord, readableKanji)
    unreadableJpWord = hanWord.reverseFilterByCharset(jpWord, readableKanji)
    partialJpWord    = list(set(jpWord) - set(readableJpWord) - set(unreadableJpWord))
    readableKanjiPer = '%.3f' % hanWord.charPercentInWord(readableKanji, jpWord)

    print
    print 'ch-common-hanzi   : %s' % len(commHanzi)
    print 'ch-readable-hanzi : %s' % len(readableHanzi)
    print 'ch-han-word       : %s' % (percent(chWord, chWord))
    print 'ch-readable       : %s' % (percent(readableChWord, chWord))
    print 'ch-partial        : %s' % (percent(partialChWord, chWord))
    print 'ch-unreadable     : %s' % (percent(unreadableChWord, chWord))
    print 'ch-percernt       : %s' % (readableHanziPer)

    print
    print 'jp-common-kanji   : %s' % len(commKanji)
    print 'jp-readable-kanji : %s' % len(readableKanji)
    print 'jp-han-word       : %s' % (percent(jpWord, jpWord))
    print 'jp-readable       : %s' % (percent(readableJpWord, jpWord))
    print 'jp-partial        : %s' % (percent(partialJpWord, jpWord))
    print 'jp-unreadable     : %s' % (percent(unreadableJpWord, jpWord))
    print 'jp-percernt       : %s' % (readableKanjiPer)
    print

#----------------------------------------------------------------------------#
def sharedWordReport():
    sharedWord = util.commList(jpWord, chWord)
    chWordConv = hanWord.getChWordConvList()
    jpWordConv = hanWord.getJpWordConvList()
    print
    print 'shared-ch-jp      : %s' % len(util.commList(chWord, jpWord))
    print 'shared-ch-jpconv  : %s' % len(util.commList(chWord, jpWordConv))
    print 'shared-jp-chconv  : %s' % len(util.commList(jpWord, chWordConv))
    print

#----------------------------------------------------------------------------#
def finalReport():
    print 
    print '---SHARED CHAR REPORT--------------------------'
    readableReport(sharedChar, sharedChar)

    print '---BASELINE REPORT-----------------------------'
    readableReport(baselineHanzi, baselineKanji)

    print '---TOPLINE REPORT------------------------------'
    readableReport(toplineHanzi, toplineKanji)

    print '---SHARED WORD REPORT--------------------------' 
    sharedWordReport()

#----------------------------------------------------------------------------#
if __name__ == '__main__':
    finalReport()

#----------------------------------------------------------------------------#
